package com.yexiao.cofe.business.pojo;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.yexiao.core.util.JSONUtil;
import com.yexiao.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 商品类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("product")
public class Product {

    /**
     * 自增主键
     */
    @TableId(type = IdType.AUTO)
    public int id;

    /**
     * 商品业务ID
     */
    @TableField("product_id")
    public int productId;

    /**
     * 商品名称
     */
    @TableField("name")
    public String name;

    /**
     * 商品描述
     */
    @TableField("description")
    public String description;

    /**
     * 分类ID
     */
    @TableField("category_id")
    public int categoryId;

    /**
     * 基础价格
     */
    @TableField("base_price")
    public BigDecimal basePrice;

    /**
     * 图片URL数组
     */
    @TableField("image")
    public String image;

    /**
     * 排序权重
     */
    @TableField("sort")
    public int sort;

    /**
     * 逻辑删除（0-未删除,1-已删除）
     */
    @TableField("is_del")
    @TableLogic
    public int isDel;

    /**
     * 创建时间
     */
    @TableField("create_time")
    public String createTime;

    /**
     * 更新时间
     */
    @TableField("update_time")
    public String updateTime;

    public static final String ID = "id";
    public static final String PRODUCT_ID = "product_id";
    public static final String NAME = "name";
    public static final String DESCRIPTION = "description";
    public static final String CATEGORY_ID = "category_id";
    public static final String BASE_PRICE = "base_price";
    public static final String IMAGE = "image";
    public static final String STATUS = "status";
    public static final String SORT = "sort";
    public static final String IS_DEL = "is_del";
    public static final String CREATE_TIME = "create_time";
    public static final String UPDATE_TIME = "update_time";

    public static JSONObject getBaseMsg(Product product) {
        List<String> imageUrlList = new ArrayList<>();
        String image = product.getImage();
        if(StrUtil.isNotEmpty(image)) {
            imageUrlList.addAll(Arrays.asList(image.split(",")));
        }

        return JSONUtil.of(
                "productId", product.getProductId(),
                "name", product.getName(),
                "description", product.getDescription(),
                "category_id", product.getCategoryId(),
                "imageUrlList", imageUrlList,
                "basePrice", product.getBasePrice(),
                "sort", product.getSort(),
                "create_time", product.getCreateTime(),
                "update_time", product.getUpdateTime()
        );
    }
}